/*
 * Copyright (C) 2011 Michael Widmer <widmm8@bfh.ch>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.aeneas.core.io;

import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import java.io.FileDescriptor;
import java.io.IOException;

/**
 * This class allows the recording of an audio file and save it to the file system.
 * @author Michael Widmer <widmm8@bfh.ch>
 */
public class AudioRecorder extends Service {

    public class AudioRecorderServiceBinder extends Binder {

        public AudioRecorder getService() {
            return AudioRecorder.this;
        }
    }
    private final AudioRecorderServiceBinder mBinder = new AudioRecorderServiceBinder();

    private MediaRecorder mRecorder;
    private boolean mRecording;
    private String LOG_TAG = "AudioRecorder";
    
    @Override
    /**
     * {@inheritDoc}
     */
    public void onCreate() {
        super.onCreate();
        
        mRecording = false;
        
        mRecorder = new MediaRecorder();
        reset();
    }
    
    @Override
    /**
     * {@inheritDoc}
     */
    public void onStart(Intent intent, int startId) {
        handleCommand(intent);
    }

    @Override
    /**
     * {@inheritDoc}
     */
    public int onStartCommand(Intent intent, int flags, int startId) {
        handleCommand(intent);
        return START_STICKY;
    }
    
    @Override
    /**
     * {@inheritDoc}
     */
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
    
    @Override
    /**
     * {@inheritDoc}
     */
    public void onDestroy() {
        if(mRecording) {
            mRecorder.stop();
        }
        mRecorder.release();
    }
    
    private void handleCommand(Intent intent) {
        if(!intent.hasExtra("outputPath")) {
            return;
        }
        String path = intent.getExtras().getString("outputPath");
        record(path);
    }
    
    /**
     * Starts recording and saves the file in the specified path.
     * @param path
     */
    public void record(String path) {
        if(mRecording) {
            return;
        }
        mRecorder.setOutputFile(path);
        record();
    }
    
    /**
     * Starts recording and saves the file in the specified file.
     * @param path
     */
    public void record(FileDescriptor fd) {
        if(mRecording) {
            return;
        }
        mRecorder.setOutputFile(fd);
        record();
    }
    protected void record() {
        try {
            mRecorder.prepare();
            mRecording = true;
            mRecorder.start();
        } catch (IllegalStateException ex) {
            Log.e(LOG_TAG, "Called 'prepare' before passing the file-path!");
        } catch (IOException ex) {
            Log.w(LOG_TAG, "IO-Erro while preparing saving path for audio-file: "+ex.getMessage());
        }
    }
    
    /**
     * Stops the current recording.
     */
    public void stop() {
        if(!mRecording) {
            return;
        }
        mRecorder.stop();
        reset();
        mRecording = false;
    }
    
    /**
     * Indicates if class is currently recording.
     * @return 
     */
    public boolean isRecording() {
        return mRecording;
    }
    
    /**
     * Used to reset settings before recording another audio file.
     */
    protected void reset() {
        if(mRecording) {
            return;
        }
        mRecorder.reset();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    }
}
